<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Menu extends ORM {

    protected $_has_many = array(
        'childs' => array(
            'model' => 'menu',
            'foreign_key' => 'parent_id'
        )
    );

    public function rules() {
        return array(
            'title' => array(
                array('max_length', array(':value', 128)),
            ),
            'uri' => array(
                array('max_length', array(':value', 512)),
            ),
        );
    }

    public function create(Validation $validation = NULL) {
        
        $this->pos = $this->_db
                ->query(Database::SELECT,
                        'SELECT MAX(pos) AS pos'
                        . ' FROM ' . $this->_table_name
                        . ' WHERE parent_id = ' . $this->parent_id)
                ->get('pos') + 1;

        parent::create($validation);
    }

}